import addaddresslistModel from "../model/addressinfosModel"
export default {
    namespaced: true,
    state: {
        addresslist: [],
        addressid: null
    },
    mutations: {
        addaddresslist(state, playload) {
            state.addresslist = [...state.addresslist, playload]
        },
        innitaddresslist(state, playload) {
            state.addresslist = playload
        },
        changeisDefault(state, playload) {
            state.addresslist.filter(item => item.id == playload)[0].isDefault = false
        },
        changeid(state, playload) {
            state.addressid = playload
        }
    },
    actions: {
        async addaddresslist(context, playload) {
            //判断选中的是不是默认是的把其他的改变一下
            let cb = playload.success
            delete playload.success
            if (playload.isDefault) {
                let { data: list3 } = await addaddresslistModel.query({
                    isDefault: true
                })
                let item = list3[0]
                await addaddresslistModel.updata(item.id, {
                    isDefault: false
                })
                context.commit("changeisDefault", item.id)
            }

            let { data: list } = await addaddresslistModel.add({
                ...playload
            })
            let { data: list4 } = await addaddresslistModel.query({
                isDefault: true
            })
            console.log(list4)
            context.commit("addaddresslist", list)
            context.commit("changeid", list4[0].id)

            if (cb) {
                cb()
            }
        },
        async innitaddresslist(context, playload) {
            let { data: list2 } = await addaddresslistModel.query({
                ...playload
            })
            let { data: list4 } = await addaddresslistModel.query({
                isDefault: true
            })
            context.commit("innitaddresslist", list2)
            context.commit("changeid", list4[0].id)
        }
    }
}